<%@taglib prefix="html" uri="struts-html" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="html" uri="struts-html" %>
<%@taglib prefix="c" uri="jstl" %>
<%@taglib prefix="fn" uri="jstl-fn" %>

    <jsp:include page="Templates/metas.jsp"/>
        <script src="../js/jquery-1.7.1.min.js" type="text/javascript"></script>
		<script src="../js/jquery.placeholder-1.0.4.js" type="text/javascript"></script>
		<script src="../js/jquery-ui-1.8.17.custom.min.js" type="text/javascript"></script>
		<script src="../js/callback.js" type="text/javascript"></script>
		<link rel="stylesheet" href="../css/jquery-ui-1.8.17.custom.css" media="all" type="text/css" />

    <body id="body_admin">
        <!--[if lt IE 7]>
            <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
        <![endif]-->
       <jsp:include page="Templates/header.jsp"/>
       <section id="container_admin">
       <jsp:include page="Templates/menuForaneo.jsp"/>
       
        <div class="content_admin">
   
        	<html:form action='/administrador/ReporteArriboVal' method='post'>
        	
     		<input type="hidden" id="cve_Estatus_Arribo" name="cve_Estatus_Arribo" value="${cve_Estatus_Arribo}" />
			<input type="hidden" id="nip" name="nip" value="${nip}" />

			<input type="hidden" id="hd_FileExport" name="hd_FileExport" value="${hd_FileExport}" />
			
			<input type="hidden" id="hd_fecha_inicio" name="hd_fecha_inicio" value="${hd_fecha_inicio}" />
			<input type="hidden" id="hd_fecha_final" name="hd_fecha_final" value="${hd_fecha_final}" />
			<input type="hidden" id="hd_estatus" name="hd_estatus" value="${hd_estatus}" />
			<input type="hidden" id="hd_nip" name="hd_nip" value="${hd_nip}" />
			<!-- Sumatorias -->
			<input type="hidden" id="hd_sumMontoTotal" name="hd_sumMontoTotal" value="${hd_sumMontoTotal}" />
			<input type="hidden" id="hd_sumRegistroTotal" name="hd_sumRegistroTotal" value="${hd_sumRegistroTotal}" />
			
        	<div id="contIdArribo" >
        	<table class="table_reg" style="text-align: left;">
        	<caption>Reporte de operaciones</caption>
        	</table>
        	<br>
        	<br>
        	
        	<table class="table_reg" style="text-align: left;">
        		<tr>
	        			<td ><span class="txt_red">*</span>Fecha de: </td>
	        			<td><input type="text" id="fecha_inicio" size="10" class="datepicker" name="fecha_inicio" readonly value="${fecha_inicio}"></td>
	        			<td ></td>
	        			<td ><span class="txt_red">*</span>Fecha hasta: </td>
	        			<td ><input type="text" id="fecha_final" size="10" class="datepicker" name="fecha_final" readonly  value="${fecha_final}"></td>
	            </tr>
	           	<tr>
	        			<td></td>
	        			<td></td>
	        			<td ></td>
	        			<td ></td>
	        			<td></td>
	            </tr>
	        	<tr>
	        			<td>Nip Usuario: </td>
	        			<td>
	        			<select id="listUsuario" name="list_Usuario" >
	        					<option value="">Todos</option>
	        					<c:forEach items="${list_Usuario}" var="list_Usuario" >
	        						<option value="${list_Usuario.nip}" >${list_Usuario.nip}</option>
	        					</c:forEach>
	        				</select>
	        			</td>
	        			<td ></td>
	        			<td >Estatus: </td>
	        			<td>
	        			<select id="listEstatusArribo" name="EstatusArribo" onchange='CondicionEstatus()'>
	        					<option value="0"></option>
	        					<c:forEach items="${EstatusArribo}" var="EstatusArribo" >
	        						<option value="${EstatusArribo.cve_Estatus_Arribo}" >${EstatusArribo.descripcion}</option>
	        					</c:forEach>
	        				</select>
	        			</td>
	            </tr>
				<tr>
	        			<td   ></td>
	        			<td   ></td>
	        			<td >
	        			<input type="image" src="../img/btn_buscar.png" onclick="return ValidarForm()"   >
	        			</td>
	        			<td  >  			
	        			</td>
	        			<td   ></td>
	            </tr>	
				
			</table>
			</div>

        	<br>
        	<br>
        	
        	<br>
        	<br>
        	<br>
        	<br>
        
        	<table class="table_reg" style="text-align: left;">
        		<caption>Reporte</caption>
        		<tr>
        			<th colspan="2" class="txt_red"> <html:errors /> </th>
        		</tr>
        		<tr>
        			<td>
        			<div id="dvData">
        				<table id="ReporteArribo"  class="table_datosRed">
        				<thead>
        					<tr>
        						<th>ID Arribo</th>
        						<th>NIP</th>
        						<th>Gu&iacute;a de arribo</th>
        						<th>Status</th>
        						<th>Descripci&oacute;n de la mercanc&iacute;a</th>
        						<th>Fecha</th>
        						<th>Cantidad de piezas</th>
        						<th>Valor de la mercanc&iacute;a</th>
        						<th>Peso volum&eacute;trico</th>
        						<th>Total de cargos</th>
        					</tr>     
        				</thead>
        				<tbody>   
    			
        					<c:forEach items="${list_RepArribo}" var="list_RepArribo"> 
	        					<tr class="body_table">
	        						<td>${list_RepArribo.cve_arribo}</td>
	        						<td>${list_RepArribo.nip}</td>
	        						<td>${list_RepArribo.cve_guia_arribo}</td>
	        						<td>${list_RepArribo.descripcion_status}</td>
	        						<td>${list_RepArribo.desc_mercancia}</td>
	        						<td>${list_RepArribo.fecha_arribo}</td>
	        						<td>${list_RepArribo.num_paquetes}</td>
	        						<td>${list_RepArribo.valor_mercancia_usd}</td>
	        						<td>${list_RepArribo.peso_volumetrico_total_kg}</td>
	        						<td>${list_RepArribo.total_pagado}</td>
	        						</tr>  
        					</c:forEach>
        					
        					
        				</tbody>
        				</table>
        				</div>
        			</td>
        		</tr>
        	</table>
      
 			<br>
        	<br>
			<table class="table_reg" style="text-align: left;">
        		<tr>
        		</tr>
        		<tr>
        			<td>
      
        				<table class="table_datosRed">

        					<tr>
	        					<td>
	        					<input type="image" src="../img/excel.png" style=" with:40px;height:40px "  onclick='ExportarReporte(2)'   >
	        					&nbsp;
	        					<input type="image" src="../img/icono-pdf3.png" style=" with:40px;height:40px "  onclick='ExportarReporte(1)'   >
	        					</td>
	        					<td>
	        					</td>
        					</tr>            				
        				</table>
        			</td>
        		</tr>
        	</table>
      		</html:form>
		</div>
      		
        	<script type="text/javascript">
			
        	function submit(cve,acc){
				document.getElementById("cve_arribo").value = cve;
				document.forms['arribosForm'].submit();
				return true;
			};
				
			$(document).ready(function() {
        		
        		
        		var NumFilas = 0;
				NumFilas = $("#ReporteArribo").find("tr").length;
				
				if(NumFilas > 1){
					CalcularTotales();
				}
				
				
        	});
			
			function CalcularTotales(){
				var hd_sumMontoTotal = 0;
				var hd_sumRegistroTotal = 0;
					
				$('#ReporteArribo tbody tr').each(function() {
					var monto = $(this).children('td').eq('9').html();
					
					hd_sumMontoTotal += parseFloat(monto);
					hd_sumRegistroTotal += parseInt(1);
				
					});
					$("#hd_sumMontoTotal").val(hd_sumMontoTotal);
					$("#hd_sumRegistroTotal").val(hd_sumRegistroTotal);
					
					if(hd_sumRegistroTotal > 0){
						//10 colums
						var cont = $("#ReporteArribo");
					     cont.append('<tr align="center" >' +
		                                '<td ><label id="lbRegTotal" name="lbRegTotal">Registros: </label></td>' +
		                                '<td ><label id="lbsumRegTotal" name="lbsumRegTotal">'+ hd_sumRegistroTotal +'</label></td>' +
		                                '<td></td>' +
		                                '<td ></td>' +
		                                '<td></td>' +
		                                '<td ></td>' +
		                                '<td></td>' +
		                                '<td ></td>' +
		                                '<td ><label id="lbsumTotal" name="lbsumTotal">Total en USD: </label></td>' +
		                                '<td><label id="lbsumMontoTotal" name="lbsumMontoTotal">'+ hd_sumMontoTotal +'</label></td>' +
		                                '</tr>' 
		                                );
					}
			};
			
				function ValidarForm(){
					
					$("#hd_nip").val($("#listUsuario option:selected").text());
					$("#hd_estatus").val($("#listEstatusArribo").val());
					$("#hd_fecha_inicio").val($("#fecha_inicio").val());
					$("#hd_fecha_final").val($("#fecha_final").val());
					
					$("#nip").val($("#listUsuario option:selected").text());
					$("#cve_Estatus_Arribo").val($("#listEstatusArribo").val());
					
					fecha_inicio = $("#fecha_inicio").val();
					fecha_final = $("#fecha_final").val();
					nip = $("#nip").val();
					
					
			        if ((fecha_inicio.replace(/^\s+|\s+$/g, "")).length == 0 &&
			        	document.getElementById('fecha_inicio').value == ''  &&
			        	(fecha_final.replace(/^\s+|\s+$/g, "")).length == 0  &&
			        	document.getElementById('fecha_final').value == ''   &&
			        	(nip.replace(/^\s+|\s+$/g, "")).length == 0  &&
			        	document.getElementById('nip').value == ''   &&
			        	$("#listEstatusArribo").val() == "0" 
			        ) {
			           
			        	alert('Debe Capturar almenos un filtro');
			        	
			            return false;
			        }
					
					
					return true;
				};

				function ExportarReporte(tipo){
					
					var NumFilas = 0;
					NumFilas = $("#ReporteArribo").find("tr").length;
					
					if(NumFilas > 1){
						$("#hd_FileExport").val(tipo);
						if(tipo==3){
							document.forms['reporteArriboForm'].submit();
						}
						return true;
					}else{
						alert('No existen registros que exportar');
			            return false;
					}
				};

					//http://jsfiddle.net/kublaios/8ZQN4/1/
					
					/*
					//Genera Pdf con Jquery
					//getting values of current time for generating the file name
			        var dt = new Date();
			        var day = dt.getDate();
			        var month = dt.getMonth() + 1;
			        var year = dt.getFullYear();
			        var hour = dt.getHours();
			        var mins = dt.getMinutes();
			        var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
			        //creating a temporary HTML link element (they support setting file names)
			        var a = document.createElement('a');
			        //getting data from our div that contains the HTML table
			        var data_type = 'data:application/vnd.ms-excel';
			        var table_div = document.getElementById('dvData');
			        var table_html = table_div.outerHTML.replace(/ /g, '%20');
			        a.href = data_type + ', ' + table_html;
			        //setting the file name
			        a.download = 'Reporte_Arribos' + postfix + '.xls';
			        //triggering the function
			        a.click();
			        //just in case, prevent default behaviour
			        e.preventDefault();
			        */
        	</script>
        	
        </section>